package com.edt.ichannel.api.service.impl;

import cc.edt.iceutils2.string.IceStringUtils;
import cc.edt.iceutils3.date.IceDateOperationUtils;
import cc.edt.iceutils3.random.IceIdGenerateUtils;
import com.edt.ichannel.api.common.constant.CommonConstant;
import com.edt.ichannel.api.common.entity.ActionResult;
import com.edt.ichannel.api.entiy.local.*;
import com.edt.ichannel.api.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
@Slf4j
public class OnlineOrderServiceImpl implements OnlineOrderService {
    @Resource
    private SelectFirstMoneyService selectFirstMoneyService;
    @Resource
    private OrderChannelService orderChannelService;
    @Resource
    private MerchantService merchantService;
    @Resource
    private ComputeService computeService;
    @Resource
    private OrderCreditCKService orderCreditCKService;
    @Resource
    private OrderCreditKdService orderCreditKdService;
    @Resource
    private EdtApiService edtApiService;
    @Resource
    private PushUseridService pushUseridService;
    @Override
    public void computCommissionSingle(OrderChannel orderChannel) {
        if(orderChannel != null){
            ActionResult result = selectFirstMoneyService.selectFirstMoneyCkC(orderChannel);
            if(result.isSuccess()){
                OrderCreditCK orderCreditCK = (OrderCreditCK) result.getResultList();
                if(orderCreditCK != null){
                    if(IceStringUtils.isBlank(orderCreditCK.getChannelId())){
                        orderChannelService.setExpansionTypeAndLevl(orderCreditCK);
                    }
                    if(orderChannel.getSettletType() != null){
                        if(CommonConstant.SETTLET_TYPE_YJ_STR.equals(orderChannel.getSettletType())){
                            orderCreditCK.setOverState(CommonConstant.FLOW_OVER);
                            orderCreditCK.setRemark("CK 月结订单不计算佣金");
                        }
                    }else{

                        Integer settletType = merchantService.selectSettletTypeByExapnsionId(orderCreditCK.getChannelId());
                        if(CommonConstant.STAFF_TYPE_QIANY == orderCreditCK.getChannelType() && settletType != null
                                && settletType.equals(CommonConstant.SETTLET_TYPE_YJ) ){
                            orderCreditCK.setOverState(CommonConstant.FLOW_OVER);
                            orderCreditCK.setRemark("CK 签约月结订单不计算佣金");
                        }
                    }
                    if(orderCreditCK.getOverState() != CommonConstant.FLOW_OVER){
                        if(IceDateOperationUtils.betweenDays(orderCreditCK.getOrderDate(),orderCreditCK.getFirstDate()) > 60){
                            orderCreditCK.setOverState(CommonConstant.FLOW_OVER);
                            orderCreditCK.setRemark("C CK 首充时间比订单时间大了 60天以上");
                        }else{
                            computeService.computCommissionCKC(orderCreditCK);
                            if(orderCreditCK.getCommissionMoney() != null){
                                orderCreditCK.setOverState(CommonConstant.FLOW_OVER);
                                orderCreditCK.setRemark("C CK 计算佣金成功");
                            }else{
                                orderCreditCK.setOverState(CommonConstant.FLOW_NOT_OVER);
                                orderCreditCK.setRemark("C CK 没有匹配到规则");
                            }
                        }
                    }
                    orderCreditCK.setNeedPushUserId(2);
                    orderCreditCK.setAddtime(new Date());
                    orderCreditCKService.saveOrUpdateOrderCreditCk(orderCreditCK);
                }
            }
        }
    }

    @Override
    public void computCommissionRelation(OrderChannel orderChannel) {
        ActionResult result = selectFirstMoneyService.selectFirstMoneyKdC(orderChannel);
        if(result.isSuccess()){
            OrderCreditKd orderCreditKd = (OrderCreditKd) result.getResultList();
            if(orderCreditKd != null){
                if(IceStringUtils.isBlank(orderCreditKd.getChannelId())){
                    orderChannelService.setExpansionTypeAndLevl(orderCreditKd);
                }
                if(orderChannel.getSettletType() != null){
                    if(CommonConstant.SETTLET_TYPE_YJ_STR.equals(orderChannel.getSettletType())){
                        orderCreditKd.setOverState(CommonConstant.FLOW_OVER);
                        orderCreditKd.setRemark("KD 月结订单不计算佣金");
                    }
                }else{
                    Integer settletType = merchantService.selectSettletTypeByExapnsionId(orderCreditKd.getChannelId());
                    if(CommonConstant.STAFF_TYPE_QIANY == orderCreditKd.getChannelType() && settletType != null
                            && settletType.equals(CommonConstant.SETTLET_TYPE_YJ) ){
                        orderCreditKd.setOverState(CommonConstant.FLOW_OVER);
                        orderCreditKd.setRemark("KD 签约月结订单不计算佣金");
                    }
                }
                if(orderCreditKd.getOverState() != CommonConstant.FLOW_OVER){
                    if(IceDateOperationUtils.betweenDays(orderCreditKd.getOrderDate(),orderCreditKd.getFirstDate()) > 60){
                        orderCreditKd.setOverState(CommonConstant.FLOW_OVER);
                        orderCreditKd.setRemark("C KD 首充时间比订单时间大了 60天以上");
                    }else{
                        computeService.computCommissionKDC(orderCreditKd);
                        if(orderCreditKd.getCommissionMoney() != null){
                            orderCreditKd.setOverState(CommonConstant.FLOW_OVER);
                            orderCreditKd.setRemark("C KD 计算佣金成功");
                        }else{
                            orderCreditKd.setOverState(CommonConstant.FLOW_NOT_OVER);
                            orderCreditKd.setRemark("C KD 没有匹配到规则");
                        }
                    }
                }
                orderCreditKd.setAddtime(new Date());
                orderCreditKdService.saveOrUpdateOrderCreditKd(orderCreditKd);
            }

        }
    }

    @Override
    @Transactional
    public void selectUserIdRelation(OrderCreditCK orderCreditCK) {
        String userId = edtApiService.selectUserId(orderCreditCK.getServiceCode(),orderCreditCK.getOrderCode(),orderCreditCK.getOrderDate());
        log.info("------selectUserIdRelation :"+orderCreditCK.getServiceCode()+"查询出来的userid "+userId);
        if(IceStringUtils.isNotBlank(userId)){
            pushUserIdToTYXXPT(orderCreditCK.getServiceCode(),orderCreditCK.getOrderCode(),userId);
        }else{
            log.info("号码 ："+orderCreditCK.getServiceCode()+ "没有查询到userId");
        }
    }

    private void pushUserIdToTYXXPT(String serviceCode, String orderCode, String userId){
        PushUserid pushUserid = new PushUserid();
        pushUserid.setId(IceIdGenerateUtils.toSerialCode(32));
        pushUserid.setServiceCode(serviceCode);
        pushUserid.setOrderCode(orderCode);
        pushUserid.setUserId(userId);
        pushUserid.setPushDate(new Date());
        ActionResult result = edtApiService.getRhKdByNumberTel(orderCode,serviceCode);
        if(result.isSuccess()){
            //修改发送状态
            pushUserid.setState(CommonConstant.PUSH_USERT_ID_SUCCESS);
            pushUserid.setRemark("推送成功");
        }else{
            pushUserid.setState(CommonConstant.PUSH_USERT_ID_FAIL);
            pushUserid.setRemark(result.getMessage());
        }
        pushUseridService.savePushUserid(pushUserid);
    }


}
